//
//  SHRealNameViewController.m
//  JiFengDaRen
//
//  Created by SH_iOS on 2017/6/5.
//  Copyright © 2017年 SH_iOS. All rights reserved.
//

#import "SHCertificationCell.h"
#import "SHTextFieldCell.h"
#import "SHLabelCell.h"
#import "SHNerAreaList.h"
#import "SHRealNameViewController.h"
#import "PictureExampleViewController.h"
#import "PhotoViewController.h"
#import "SHCustomView.h"

@interface SHRealNameViewController ()<UITableViewDelegate,UITableViewDataSource,CertificationCellDelegate,SHNerAreaListDelegate,PhotoViewControllerDelegate>

@property(nonatomic,strong)UITableView *tableView;
@property(nonatomic,strong)SHTextFieldCell *custNameCell;//姓名
@property(nonatomic,strong)SHTextFieldCell *certificateNoCell;//身份证号
@property(nonatomic,strong)SHTextFieldCell *userEmailCell;//用户邮箱
@property(nonatomic,strong)SHLabelCell *provinceCell;//省份
@property(nonatomic,strong)SHLabelCell *cityCell;//城市
@property(nonatomic,strong)SHCertificationCell *photoCell;//拍照



@property(nonatomic,strong)SHCustomView  *customView;
@property(nonatomic,strong)NSString * custId;//商户号
@property(nonatomic,strong)NSString * custMobile;//手机号
@property(nonatomic,strong)NSString * custName;//商户名
@property(nonatomic,strong)NSString * certificateNo;//身份证号
@property(nonatomic,strong)NSString * userEmail;//邮箱
@property(nonatomic,strong)NSString * province;//省份名字
@property(nonatomic,strong)NSString * city;//城市名字
@property(nonatomic,strong)NSString * provinceId;//省份编号
@property(nonatomic,strong)NSString * cityId;//城市编号

@property(nonatomic,strong)NSString * cardFront;//身份证正面照
@property(nonatomic,strong)NSString * cardBack;//身份证反面照
@property(nonatomic,strong)NSString * cardHandheld;//手持身份证

@property(nonatomic,strong)NSString * hold_idcard_pic;////手持身份证url
@property(nonatomic,strong)NSString * idcard_front_pic;//身份证正面照url
@property(nonatomic,strong)NSString * idcard_other_side_pic;//身份证反面照url





@end

@implementation SHRealNameViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self configUI];
    
}

#pragma mark -- 初始化界面
- (void)configUI{
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"实名认证";
    [self.view addSubview:self.tableView];

    [self createQuickIdentity];
}


#pragma mrak - 实名认证快速认证
- (void)createQuickIdentity{
    
    UIBarButtonItem *quickIdentityItem = [[UIBarButtonItem alloc]initWithTitle:@"普通认证" style:UIBarButtonItemStyleDone target:self action:@selector(identityAction)];
    [quickIdentityItem setTitleTextAttributes:@{
                                                NSFontAttributeName:KSHSystemFont(15),
                                                NSForegroundColorAttributeName:KREDCOLOR
                                                
                                                } forState:UIControlStateNormal];
    self.navigationItem.rightBarButtonItem = quickIdentityItem;
}

#pragma mark
- (void)identityAction{
    
    SHIdentityViewController *identityVc = [[SHIdentityViewController alloc]init];
    
    [self.navigationController pushViewController:identityVc animated:YES];
}


- (UITableView *)tableView{
    
    
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0,0, KScreenWidth, KScreenHeight - KNAVIGATIONBARHEIGHT) style:UITableViewStyleGrouped];
        _tableView.dataSource = self;
        _tableView.delegate  = self;
        _tableView.scrollEnabled = NO;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        
    }
    return  _tableView;
}


#pragma mark --- UITableView && Delegate Datasource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return   5;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.row == 0){
        self.custNameCell = [[SHTextFieldCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@""];
        self.custNameCell.labelText = @"*真实姓名";
        self.custNameCell.placeholder = @"请如实填写";
        self.custNameCell.selectionStyle = UITableViewCellSelectionStyleNone;
        return self.custNameCell;
    }else if (indexPath.row == 1){
        
        self.certificateNoCell = [[SHTextFieldCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@""];
        self.certificateNoCell.labelText = @"*证件号码";
        self.certificateNoCell.placeholder = @"请输入身份证号码";
        self.certificateNoCell.selectionStyle = UITableViewCellSelectionStyleNone;
        return self.certificateNoCell;
//    }else if (indexPath.row == 2){
//        self.userEmailCell = [[SHTextFieldCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@""];
//        self.userEmailCell.labelText = @"*邮       箱";
//        self.userEmailCell.placeholder = @"注册者邮箱";
//        self.userEmailCell.selectionStyle = UITableViewCellSelectionStyleNone;
//        self.userEmailCell.contentTf.keyboardType = UIKeyboardTypeEmailAddress;
//        return  self.userEmailCell;
    }else if (indexPath.row == 2){
        self.provinceCell = [[SHLabelCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@""];
        self.provinceCell.labelText = @"*所在省份";
        self.provinceCell.placeholder = @"请选择";
        self.provinceCell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        return  self.provinceCell;
    }else if (indexPath.row == 3){
        
        self.cityCell = [[SHLabelCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@""];
        self.cityCell.labelText = @"*所在城市";
        self.cityCell.placeholder = @"请选择";
        self.cityCell.selectionStyle = UITableViewCellSelectionStyleNone;
          
        return self.cityCell;
        
    }else{
        
        self.photoCell = [[SHCertificationCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@""];
        self.photoCell.selectionStyle = UITableViewCellSelectionStyleNone;
        self.photoCell.delegate = self;
        return self.photoCell;
        
    }
    
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.row < 4) {
        return __I6Height(44.f);
        
    }else{
        return KScreenHeight - KNAVIGATIONBARHEIGHT - __I6Height(44.f) * 4 ;
    }
    
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return __I6Height(10);
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, KScreenWidth, __I6Height(10))];
    view.backgroundColor = [UIColor colorWithWhite:242.f/255.f alpha:1];
    view.layer.borderColor = KLINECOLOR.CGColor;
    view.layer.borderWidth = 0.5;
    return view;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.row == 2) {
        if (!self.custNameCell.contentTf.text) {
            [self.view makeToast:@"请填写真实姓名！" duration:1.5 position:@"center"];
            return;
        }
        
        if (!self.certificateNoCell.contentTf.text) {
            [self.view makeToast:@"请填写身份证号码！" duration:1.5 position:@"center"];
            return;
        }
       
        
        [self showAreaList];
    }else if (indexPath.row == 3){
        [self showAreaList];
        
    }
    
    
}

#pragma mark - 显示地址列表
- (void)showAreaList{
    
    [self.view endEditing:YES];
    SHNerAreaList *areaList = [[SHNerAreaList alloc]init];
    areaList.delegate = self;
    [areaList show];
    
}

#pragma mark - 地址选择代理
- (void)pickerArea:(SHNerAreaList *)pickerArea province:(NSString *)province city:(NSString *)city  provinceId:(NSString *)provinceId cityId:(NSString *)cityId  area:(NSString *)area{
    
    [self.provinceCell contentLabelText:province];
    [self.cityCell contentLabelText:city];
    self.province = province;
    self.city = city;
    self.provinceId = provinceId;
    self.cityId = cityId;
    SHLog(@"%@----%@",province,city);
  
    
}

#pragma mark -- CertificationCell delegate    传出cell按钮点击事件   上传照片
- (void)updatePhoto:(SHCertificationCell *)cell customView:(SHCustomView *)customView{
    [self.view endEditing:YES];
    
    if (!self.provinceId) {
        [self.view makeToast:@"请选择省份和城市！" duration:1.5 position:@"center"];
        return;
    }
    if (!self.cityId) {
        [self.view makeToast:@"请选择省份和城市！" duration:1.5 position:@"center"];

        return;
    }
    
    _customView = customView;
    
    self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"" style:UIBarButtonItemStyleDone target:nil action:nil];
    PictureExampleViewController *picture = [[PictureExampleViewController alloc]init];
    picture.index = customView.index;
    [self.navigationController pushViewController:picture animated:YES];
    
}

#pragma mark --- 立即认证 上传实名认证信息
- (void)certificationBtnClick{
    
    [self requestShForCertification];

    
}
#pragma mark -- 优惠世家实名认证
- (void)requestShForCertification{

    
    self.custName = self.custNameCell.contentTf.text;
    self.certificateNo = self.certificateNoCell.contentTf.text;
    self.userEmail = self.userEmailCell.contentTf.text;
    self.cardHandheld =  self.hold_idcard_pic;
    self.cardFront = self.idcard_front_pic ;
    self.cardBack =  self.idcard_other_side_pic;
    if (self.custName.length == 0) {
        
        [self.view makeToast:@"请输入您的真实姓名" duration:2.f position:@"center"];

        return;
    }
    if (!self.certificateNo) {
        
        [self.view makeToast:@"请输入您的身份证号码" duration:2.f position:@"center"];

        return;
    }else{
        if (self.certificateNo.length != 15 && self.certificateNo.length != 18) {
            
            [self.view makeToast:@"身份证号码不合法" duration:2.f position:@"center"];

            return;
        }
    }
   
    
    if (!self.provinceId) {
        
        [self.view makeToast:@"请选择省份" duration:2.f position:@"center"];

        return;
    }
    
    if (self.cardFront.length == 0) {
        
        [self.view makeToast:@"请上传银行卡正面照" duration:2.f position:@"center"];

        return;
    }
    
    if (self.cardBack.length == 0) {
        
        [self.view makeToast:@"请上传银行卡反面照" duration:2.f position:@"center"];

        return;
    }
    if (self.cardHandheld.length == 0) {
        
        [self.view makeToast:@"请上传手持身份证照" duration:2.f position:@"center"];

        return;
    }
    
 
    
    NSMutableDictionary *param = [[NSMutableDictionary alloc]init];
    [param setValue:self.custName forKey:@"real_name"];
    [param setValue:self.certificateNo forKey:@"credentials_number"];
    [param setValue:self.hold_idcard_pic forKey:@"hold_idcard_pic"];
    [param setValue:self.idcard_front_pic forKey:@"idcard_front_pic"];
    [param setValue:self.idcard_other_side_pic forKey:@"idcard_other_side_pic"];
    [param setValue:self.province forKey:@"province"];
    [param setValue:self.city forKey:@"city"];
    
    
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    SHLog(@"实名认证========%@",param);
    
    [MyDataService requestAFURL:KREAL_NAME_AUTH_C httpMethod:@"POST" params:param data:nil complection:^(id result) {
        SHLog(@"首瀚实名认证%@",result);
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        
        if ([result[@"success"] isEqualToNumber:@(1)]) {
            [self.view makeToast:@"上传成功！" duration:2.f position:@"center" ];
            
            dispatch_time_t delayTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2  * NSEC_PER_SEC));
            
            dispatch_after(delayTime, dispatch_get_main_queue(), ^{
                
                [self.navigationController popViewControllerAnimated:YES];
            });
            
        }else{
            
            [self.view makeToast:result[@"data"] duration:2.f position:@"center" ];
            
        }
    } failure:^(id erro) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        
    }];
    
}

#pragma mark -- 自定义相机返回图片代理
-(void)getUIImageFromPhotoViewController:(PhotoViewController *)photoViewController image:(UIImage *)image{
     [MBProgressHUD showHUDAddedTo:self.view animated:YES];

    [SHQiniuManager qiniuUploadImage:image scale:YES size:CGSizeMake(400, 400) complete:^(NSString *qnImageUrl) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];

        _customView.icon.image = image;
        switch (_customView.index) {
                case 1000:
                self.idcard_front_pic = qnImageUrl;
                break;
                case 1001:
                self.idcard_other_side_pic = qnImageUrl;
                break;
                case 1002:
                self.hold_idcard_pic = qnImageUrl;
                break;
            default:
                break;
        }
        
    }];
}

@end
